package com.rxh.controller;

import com.rxh.pojo.base.Page;
import com.rxh.pojo.base.SearchInfo;
import com.rxh.service.FinanceService;
import com.rxh.service.TransactionService;
import com.rxh.util.UserInfoUtils;
import com.rxh.utils.SystemConstant;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: 陈俊雄
 * Date: 2018/8/7
 * Time: 15:53
 * Project: Management
 * Package: com.rxh.controller
 */
@Controller
@RequestMapping(value = "/homePage")
@ResponseBody
public class HomePageController {
    @Resource
    private TransactionService transactionService;
    @Resource
    private FinanceService financeService;

    @RequestMapping(value = "/tradingToday")
    public Map<String, Integer> tradingToday() {
        Map<String, Integer> tradingToday = new HashMap<>();
        SearchInfo searchInfo = new SearchInfo();
        searchInfo.setMerId(UserInfoUtils.getMerchantId());
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        searchInfo.setStartDate(new Date(calendar.getTimeInMillis()));
        tradingToday.put("all", transactionService.getOrderCountBySearchInfo(searchInfo));
        searchInfo.setBankStatus(SystemConstant.BANK_STATUS_SUCCESS);
        tradingToday.put("success", transactionService.getOrderCountBySearchInfo(searchInfo));
        searchInfo.setExceptionStatus(SystemConstant.FULL_REFUSED);
        int refused = transactionService.getOrderCountBySearchInfo(searchInfo);
        searchInfo.setExceptionStatus(SystemConstant.PARTIAL_REFUSED);
        refused += transactionService.getOrderCountBySearchInfo(searchInfo);
        searchInfo.setExceptionStatus(SystemConstant.PROXY_REFUSED);
        refused += transactionService.getOrderCountBySearchInfo(searchInfo);
        tradingToday.put("refused", refused);
        searchInfo.setExceptionStatus(SystemConstant.FULL_REFUND);
        int refund = transactionService.getOrderCountBySearchInfo(searchInfo);
        searchInfo.setExceptionStatus(SystemConstant.PARTIAL_REFUND);
        refund += transactionService.getOrderCountBySearchInfo(searchInfo);
        tradingToday.put("refund", refund);
        return tradingToday;
    }

    @RequestMapping(value = "/todayTransactionAmount")
    public Map<String, BigDecimal> todayTransactionAmount() {
        SearchInfo searchInfo = new SearchInfo();
        searchInfo.setMerId(UserInfoUtils.getMerchantId());
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        searchInfo.setStartDate(new Date(calendar.getTimeInMillis()));
        Map<String, BigDecimal> todayTransactionAmount = new HashMap<>();
        searchInfo.setBankStatus(SystemConstant.BANK_STATUS_SUCCESS);
        todayTransactionAmount.put("success", transactionService.getOrderRmbAmountBySearchInfo(searchInfo));
        searchInfo.setChangeStatus(SystemConstant.ORDER_CHANGE_STATUS_SUCCESSFUL);
        searchInfo.setChangeTypeList(Arrays.asList(SystemConstant.FULL_REFUSED, SystemConstant.PARTIAL_REFUSED, SystemConstant.PROXY_REFUSED));
        todayTransactionAmount.put("refused", transactionService.getOrderChangeRmbAmountBySearchInfo(searchInfo));
        searchInfo.setChangeTypeList(Arrays.asList(SystemConstant.FULL_REFUND, SystemConstant.PARTIAL_REFUND));
        todayTransactionAmount.put("refund", transactionService.getOrderChangeRmbAmountBySearchInfo(searchInfo));
        return todayTransactionAmount;
    }

    @RequestMapping(value = "/backlog")
    public Map<String, Object> backlog() {
        SearchInfo searchInfo = new SearchInfo();
        searchInfo.setMerId(UserInfoUtils.getMerchantId());
        Page page = new Page();
        page.setPageNum(1);
        page.setPageSize(1);
        page.setSearchInfo(searchInfo);
        Map<String, Object> backlog = new HashMap<>();
        searchInfo.setStatus(SystemConstant.FINANCE_DRAWING_STATUS_UNAUDITED);
        backlog.put("financeDrawing", financeService.getFinanceDrawing(page).getTotal());
        searchInfo.setStatus(null);
        searchInfo.setChangeTypeList(Arrays.asList(SystemConstant.FULL_REFUND, SystemConstant.PARTIAL_REFUND));
        searchInfo.setChangeStatus(SystemConstant.ORDER_CHANGE_STATUS_UNAUDITED);
        backlog.put("refundUnaudited", transactionService.getOrderChangeCountBySearchInfo(searchInfo));
        searchInfo.setChangeStatus(SystemConstant.ORDER_CHANGE_STATUS_FAIL);
        backlog.put("refundFail", transactionService.getOrderChangeCountBySearchInfo(searchInfo));
        return backlog;
    }
}